import { InteractiveGroup } from 'three/examples/jsm/interactive/InteractiveGroup'
import { CSS2DObject } from 'three/examples/jsm/renderers/CSS2DRenderer'

export default class Labels {
  constructor (viewer) {
    this.viewer = viewer
    this.group = new InteractiveGroup(this.viewer.renderer, this.viewer.camera) // 创建交互对象
    this.viewer.scene.add(this.group)
  }
  /**
   * 添加2d标签
   * @param {*} position
   * @param {*} html html内容
   */
  // addCss2dLabel(position = { x: 0, y: 0, z: 0 }, html = "") {
  //   const div = document.createElement('div')
  //   div.style.position = 'absolute'
  //   div.innerHTML = html
  //   this.label = new CSS2DObject(div)
  //   this.label.position.set(position.x, position.y, position.z)
  //   this.viewer.scene.add(this.label)
  //   return this.label
  // }

  addCss2dLabel (position = { x: 0, y: 0, z: 0 }, html = '', parent) {
    const div = document.createElement('div')
    div.style.position = 'absolute'
    div.innerHTML = html

    const label = new CSS2DObject(div)
    label.position.set(position.x, position.y, position.z)

    // 如果未传入 parent，则默认挂载到 scene 上
    const target = parent || this.viewer.scene
    target.add(label)

    return label
  }
}
